package casa.depesos.app.com.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.support.toast.ToastUtil;

import casa.depesos.app.com.R;
import casa.depesos.app.com.databinding.ActivityExtendBinding;
import casa.depesos.app.com.dlg.ExtendDlg;
import casa.depesos.app.com.dlg.TipDlg;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.ExtendBean;
import casa.depesos.app.com.http.bean.ExtendLinkBean;
import casa.depesos.app.com.http.data.Extend;
import casa.depesos.app.com.http.data.ExtendLink;
import casa.depesos.app.com.http.data.RepayDetail;
import casa.depesos.app.com.http.req.ExtendLinkReq;
import casa.depesos.app.com.http.req.ExtendReq;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public class ExtendActivity extends BaseActivity {

    ActivityExtendBinding binding;
    String id, email;
    ExtendLink link;

    @Override
    protected boolean needPadding() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityExtendBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.top.back.setOnClickListener(v -> finish());
        binding.top.title.setText("Pago de extensión");
        binding.top.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        id = getIntent().getStringExtra("id");
        email = getIntent().getStringExtra("email");
        if (TextUtils.isEmpty(id)) {
            finish();
            return;
        } else {
            Report.show("ACTION_TT_005");
        }
        RepayDetail info = getIntent().getParcelableExtra("detail");
        String fee = getIntent().getStringExtra("fee");

        binding.amount.setText(Utils.formatNoFloat(info.remain + "", true));



        if (!TextUtils.isEmpty(fee)) {
            binding.frame.setVisibility(View.VISIBLE);
            binding.tv2.setText(Utils.formatNoFloat(fee, true));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        showLoading();
        new ExtendReq(id).req(new ReqCallback<ExtendBean>() {
            @Override
            public void callback(boolean success, ExtendBean bean) {
                if (success) {
                    show(bean.info);
                } else {
                    hideLoading();
                }
            }
        });
    }

    int way = 0;

    private void changeWay(int now) {
        if (now == 0) {
            binding.repay.setBackgroundResource(R.drawable.right_btn);
            binding.repay.setTextColor(Color.WHITE);
            binding.payed.setBackgroundResource(R.drawable.green_stroke_bg);
            binding.payed.setTextColor(0xff71D561);
        } else {
            binding.payed.setBackgroundResource(R.drawable.right_btn);
            binding.payed.setTextColor(Color.WHITE);
            binding.repay.setBackgroundResource(R.drawable.green_stroke_bg);
            binding.repay.setTextColor(0xff71D561);
            ToastUtil.showShortToast(this, "Aún no es compatible");
        }
        way = now;
    }

    private void show(Extend info) {
        getLink(null);
        binding.total.setText(Utils.formatNoFloat(info.remain + "", true));
        binding.date.setText(Utils.getDay3(info.lastDate));
        binding.fee.setText(Utils.formatNoFloat(info.amount, true));

        binding.tv.setOnClickListener(v -> {
            if(link == null){
                return;
            }
            new ExtendDlg().setParam(info, link).show(getSupportFragmentManager(), "");
        });
        binding.repay.setOnClickListener(v -> {
            Report.click("ACTION_TT_006");
            changeWay(0);
        });
        binding.payed.setOnClickListener(v -> {
            Report.click("ACTION_TT_006");
            ToastUtil.showShortToast(ExtendActivity.this, "Todavía no está abierto, estad atentos");
        });
        binding.copy.setOnClickListener(v -> {
            Utils.copy(binding.code.getText().toString(), false);
            new TipDlg().setMsg("Copiado", getString(R.string.tip2)).show(getSupportFragmentManager(), "");
            Report.click("ACTION_TT_007");
        });
        binding.help.setVisibility(View.VISIBLE);
        binding.help.setOnClickListener(v -> {
            if (way == 1) {
                Intent i = new Intent(ExtendActivity.this, HowActivity.class);
                startActivity(i);
            } else {
                new TipDlg().show(getSupportFragmentManager(), "");
            }
        });
    }

    private void toWeb() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(ExtendActivity.this, WebActivity.class);
                i.putExtra("url", link.url);
                i.putExtra("top", true);
                startActivity(i);
            }
        };
        if (link == null) {
            getLink(runnable);
        } else {
            runnable.run();
        }
    }

    private void getLink(Runnable runnable) {
        showLoading();
        new ExtendLinkReq(1, id).req(new ReqCallback<ExtendLinkBean>() {
            @Override
            public void callback(boolean success, ExtendLinkBean bean) {
                hideLoading();
                if (success) {
                    link = bean.info;
                    binding.code.setText(link.account);
                    if (runnable != null) {
                        runnable.run();
                    }
                }
            }
        });
    }
}
